package cn.Y_Qiu.org.controller;

import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.org.domain.Shop;
import cn.Y_Qiu.org.domain.ShopExamina;
import cn.Y_Qiu.org.query.ShopQuery;
import cn.Y_Qiu.org.service.IShopExaminaService;
import cn.Y_Qiu.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shopexamina")
public class ShopExaminaController {
    @Autowired
    private IShopService shopService;
    @Autowired
    private IShopExaminaService shopExaminaService;
    @Autowired
    private JavaMailSender javaMailSender;

    // 添加
    @PutMapping("/addtoshop")
    public  Ajaxresult Update(@RequestBody Shop shop){
        Long id = shop.getId();
        try {
            shopExaminaService.remove(id);
            shopService.shopsettlement(shop);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("炸了");
        }
    }

    // 删除
    @DeleteMapping("/{id}")
    public Ajaxresult delById(@PathVariable("id") Long id){
        try {
            shopExaminaService.remove(id);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me()
                    .setMessage("失败");
        }
    }
    // 查单
    @GetMapping("/{id}")
    public ShopExamina queryById(@PathVariable("id") Long id){
        return shopExaminaService.queryById(id);
    }
    // 查全
    @GetMapping
    public List<ShopExamina> queryAll(){
        return shopExaminaService.listAll();
    }
    @PostMapping
    public PageList<ShopExamina> queryPage(@RequestBody ShopQuery shopQuery){
        PageList<ShopExamina> shopExaminaPageList = shopExaminaService.queryPage(shopQuery);
        return shopExaminaPageList;
    }


    @PatchMapping
    public Ajaxresult patchremove(@RequestBody List<Long> ids){
        System.out.println("到达controller");
        try {
            shopExaminaService.patchremove(ids);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("失败");
        }

    }
    @PostMapping("/settlement")
    public Ajaxresult shopRegister(@RequestBody ShopExamina shopExamina){
        try {
            shopExaminaService.regist(shopExamina);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage(e.getMessage());
        }
    }

    @PostMapping("/sendemail")
    public Ajaxresult sendemail(@RequestBody ShopExamina shopExamina){
        System.out.println("到达发送邮件controller");
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //设置发送人
            mailMessage.setFrom("15676147696@163.com");
            //邮件主题
            mailMessage.setSubject("入驻审核通知");
            //邮件内容
            mailMessage.setText("宠物乐园商铺入驻成功");
            //收件人
            mailMessage.setTo(shopExamina.getEmail());
            javaMailSender.send(mailMessage);
            return Ajaxresult.me();
        } catch (MailException e) {
            e.printStackTrace();
            return new Ajaxresult().setMessage(e.getMessage());
        }
    }
}
